package data_structure;

/*********************************************************************
 * Class CircularStack - copy from carter sudoku version
 * 1. Support the Redo and Undo action
 *********************************************************************/
public class CircularStack
{
    /* default constructor */
    public CircularStack(){
        stackSize = CircularStackProperties .getDefaultStackSize();
        emptyStack();
    }

    /* constructor */
    public CircularStack( int inputStackSize ) {
        stackSize = inputStackSize;
        emptyStack();
    }

    /* clear the stack */
    public void emptyStack(){
        stackValues = new String[stackSize] ;
    }

    /* get the object at the top of the stack without removing it from the stack.*/
    public String Top()
    {
        String firstElement ="" ;
        if( getNumOfElement() > 0 )
        {
            firstElement = stackValues[0] ;
        }
        return firstElement ;
    }

    /* get the first value and remove it form the stack */
    public String Pop()
    {
        final int INT_VALUES_LENGTH = stackValues.length ;
        String popedValue = stackValues[0] ;

        for( int i = 0 ; i < (INT_VALUES_LENGTH-1) ; ++i )
        {
            stackValues[i] = stackValues[i+1] ;
        }

        stackValues[INT_VALUES_LENGTH-1] = null ;


        return popedValue ;
    }

    /* set a value to the stack */
    public void Push( String pushValue ){

        final int INT_VALUES_LENGTH = stackValues.length ;

        for( int i = (INT_VALUES_LENGTH-1) ; i > 0 ; --i )
        {
            stackValues[i] = stackValues[i-1] ;
        }

        stackValues[0] = pushValue ;
    }

    /* get the number of element(s) in the stack */
    public int getNumOfElement()
    {
        int numOfElement = 0 ;

        for( int i = 0 ; i < stackValues.length ; ++ i )
        {
            if( (stackValues[i] != null) && !(stackValues[i].equals("")) )
            {
                ++numOfElement ;
            }
        }

        return numOfElement ;
    }
  
    /* the array to store the stack */
    private String[] stackValues ;

    /* the pre-set size of the stack */
    private int stackSize;
    /* the default values separator */
   // private static final String DEFAULT_STR_SEP = " " ;

}
